package neu.dreamera.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.os.Build;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.List;


/**
 * Created by lijialin on 16-6-21.
 * 相机预览类
 */
@SuppressWarnings("deprecation")
public class CameraPreview extends SurfaceView implements
        SurfaceHolder.Callback,Camera.AutoFocusCallback {

    private Activity activity;//作用的activity
    public Camera camera; //相机
    public Parameters parameters; //相机参数
    public SurfaceView surfaceView;
    public SurfaceHolder surfaceHolder;
    public float screenWidth; //屏幕宽度


    /**
     * 给CameraActivity用的构造函数
     */
    public CameraPreview(Context context, SurfaceView surfaceView) {
        super(context);
        this.activity = (Activity) context;
        if(surfaceView != null) {
            this.surfaceView = surfaceView;
            this.surfaceHolder = surfaceView.getHolder();
            this.surfaceHolder.addCallback(this);
        }
        initSurfaceView();
    }


    /**
     * 初始化SurfaceView
     */
    private void initSurfaceView() {
        screenWidth  = activity.getWindowManager().getDefaultDisplay().getWidth();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.setFixedSize((int)screenWidth, (int)screenWidth);
        surfaceHolder.setKeepScreenOn(true);// 屏幕常亮
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        SetAndStartPreview(holder);
    }


    /**
     * 开始在surfaceView上的预览相机画面
     */
    public void SetAndStartPreview(SurfaceHolder holder) {
        try {
            camera = Camera.open();
            camera.setPreviewDisplay(holder);
            camera.setDisplayOrientation(getPreviewDegree(activity));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据手机方向获得相机预览画面旋转的角度
     */
    @TargetApi(Build.VERSION_CODES.FROYO)
    public int getPreviewDegree(Activity activity) {
        // 获得手机的方向
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        int degree = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degree = 90;
                break;
            case Surface.ROTATION_90:
                degree = 0;
                break;
            case Surface.ROTATION_180:
                degree = 270;
                break;
            case Surface.ROTATION_270:
                degree = 180;
                break;
        }
        return degree;
    }


    public void setCameraParameters(int width, int height) {
        parameters = camera.getParameters(); // 获取各项参数
        parameters.setPictureFormat(PixelFormat.JPEG); // 设置图片格式
        parameters.setJpegQuality(100); // 设置照片质量
        parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        Size s = getBestSupportedSize(
                parameters.getSupportedPictureSizes(), width, height);
        parameters.setPictureSize(s.width, s.height);//3120 4160
        s = getBestSupportedSize(
                parameters.getSupportedPreviewSizes(), width, height);
        parameters.setPreviewSize(s.width, s.height); // 2224 1668
    }

    @Override
    public void surfaceChanged(SurfaceHolder  holder, int format, int width, int height) {
        setCameraParameters(width, height);
        camera.setParameters(parameters);
        camera.startPreview(); // 开始预览
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (camera != null) {
            camera.release(); // 释放照相机
            camera = null;
        }
    }


    /**
     * 找出最大像素尺寸
     */
    public Size getBestSupportedSize(List<Size> sizes,int w,int h) {
        Size bestSize = sizes.get(0);
        int largestArea = bestSize.width * bestSize.height;
        for (Size s :sizes) {
            if ((float)s.width/(float)s.height == (float)h/(float)w){
                int area = s.width * s.height;
                if (area > largestArea) {
                    bestSize=s;
                    largestArea = area;
                }
            }
        }
        return bestSize;
    }

    @Override
    public void onAutoFocus(boolean arg0, Camera arg1) {}

}